﻿using RemoterCheckService;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RemoterCheckController
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string ServiceName = "Remoter Check Service";
        string ServicePath = System.AppDomain.CurrentDomain.BaseDirectory + @"\RemoterCheckService.exe";

        public MainWindow()
        {
            InitializeComponent();
        }
        #region 按钮点击
        private void Setup_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsServiceExisted(ServiceName))
                this.UninstallService(ServiceName);
            this.InstallService(ServicePath);
        }
        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsServiceExisted(ServiceName))
                this.ServiceStart(ServiceName);
        }
        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsServiceExisted(ServiceName))
                this.ServiceStop(ServiceName);
        }
        private void UnSetup_Click(object sender, RoutedEventArgs e)
        {
            if (this.IsServiceExisted(ServiceName))
            {
                this.ServiceStop(ServiceName);
                this.UninstallService(ServicePath);
            }
        }
        #endregion
        private void Test_Click(object sender, RoutedEventArgs e)
        {
            HttpServer httpServer = new HttpServer();
            httpServer.Setup();
        }


        #region 服务相关
        //判断服务是否存在
        private bool IsServiceExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        //安装服务
        private void InstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }
        }

        //卸载服务
        private void UninstallService(string serviceFilePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
            }
        }
        //启动服务
        private void ServiceStart(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }
        }

        //停止服务
        private void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }
        #endregion
    }
}
